package com.cmsz.shuang.booter.service;

import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.apache.ibatis.session.SqlSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.cmsz.shuang.booter.dao.AccessMapper;
import com.cmsz.shuang.booter.dao.RolePoMapper;
import com.cmsz.shuang.booter.dao.UserPoMapper;
import com.cmsz.shuang.booter.model.RolePo;
import com.cmsz.shuang.booter.model.UserPo;

@Service
public class ServiceApp1 {
	
	@Value("${service.name}")
	private String servicename;
	
	@Autowired
	@NotNull
	@Valid
	private  JdbcTemplate jdbcTemplate;
	
	@Autowired
	@NotNull
	@Valid
	private SqlSession sqlsession;
	
	@Autowired
	@NotNull
	@Valid
	private RolePoMapper rmapper;
	
	@Autowired
	@NotNull
	@Valid
	private AccessMapper amapper;
	
	@Autowired
	private CacheDemo cachedemo;
	
	@PostConstruct
	public void print(){
		Logger loger=LoggerFactory.getLogger(ServiceApp1.class);
		loger.info("this is service {}", servicename);
		System.out.println(this.servicename);
		List<Map<String, Object>> list=jdbcTemplate.queryForList("select * from TB_USER");
		for(Map<String,Object> m: list)
		{
			StringBuilder sb=new StringBuilder();
			sb.append("id=").append(m.get("ID"));
			sb.append(",first_name=").append(m.get("NAME"));
			System.out.println(sb.toString());
		}
		
		UserPoMapper mapper=sqlsession.getMapper(UserPoMapper.class);
		UserPo po=mapper.selectByPrimaryKey(0);
		System.out.println(po.getName()+" "+ po.getPassword());
		
		RolePo rpo=rmapper.selectByPrimaryKey(1);
		System.out.println(rpo.getName()+" ," + rpo.getDescription());
		
		List<UserPo> lis=amapper.getAllUserPo();
		for(UserPo p: lis)
		{
			System.out.println("User po :" + p.getName());
		}
		
		Long l = cachedemo.findById(100L);
		
		
	}

}
